package com.whmall.service.crm.provider;

import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.api.SubstituteInvoiceCompanyApi;
import com.whmall.component.entity.dto.CompanyKeyFieldDTO;
import com.whmall.service.crm.entity.dto.companyuser.CompanyUserKeyFieldRspDTO;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyAddReqDTO;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyQueryReqDTO;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyQueryRspDTO;
import com.whmall.service.crm.entity.dto.invoice.SubstituteInvoiceCompanyUpdateReqDTO;
import com.whmall.service.crm.module.company.service.CompanyService;
import com.whmall.service.crm.module.companyuser.CompanyUserService;
import com.whmall.service.crm.module.invoice.service.SubstituteInvoiceCompanyService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(interfaceClass = SubstituteInvoiceCompanyApi.class)
public class SubstituteInvoiceCompanyProvider implements SubstituteInvoiceCompanyApi {

    @Autowired
    private SubstituteInvoiceCompanyService substituteInvoiceCompanyService;

    @Autowired
    private CompanyUserService companyUserService;

    @Autowired
    private CompanyService companyService;

    @Override
    public List<SubstituteInvoiceCompanyQueryRspDTO> query(SubstituteInvoiceCompanyQueryReqDTO reqDTO) {
        List<SubstituteInvoiceCompanyQueryRspDTO> invoiceCompanyQueryRspDTOS = substituteInvoiceCompanyService.query(reqDTO);
        if (invoiceCompanyQueryRspDTOS == null){
            return new ArrayList<SubstituteInvoiceCompanyQueryRspDTO>();
        }
        Map<Long, CompanyUserKeyFieldRspDTO> companyUserRspDTOMap = companyUserService
                .listByIds(invoiceCompanyQueryRspDTOS.stream().map(
                e -> e.getUserId()).collect(Collectors.toList()));
        List<SubstituteInvoiceCompanyQueryRspDTO> companyQueryRspDTOS = invoiceCompanyQueryRspDTOS
                .stream().map(invoiceCompanyQueryRspDTO -> {
            if (companyUserRspDTOMap.containsKey(invoiceCompanyQueryRspDTO.getUserId())) {
                invoiceCompanyQueryRspDTO.setUserName(companyUserRspDTOMap
                        .get(invoiceCompanyQueryRspDTO.getUserId()).getName());
            }
            return invoiceCompanyQueryRspDTO;
        }).collect(Collectors.toList());
        return companyQueryRspDTOS;
    }

    @Override
    public PageRspDTO<SubstituteInvoiceCompanyQueryRspDTO> queryPage(SubstituteInvoiceCompanyQueryReqDTO reqDTO, PageReqDTO pageReqDTO) {
        PageRspDTO<SubstituteInvoiceCompanyQueryRspDTO> pageRspDTO = substituteInvoiceCompanyService
                .queryPage(reqDTO, pageReqDTO);
        List<SubstituteInvoiceCompanyQueryRspDTO> invoiceCompanys = pageRspDTO.getRecords();
        if (invoiceCompanys == null){
            return pageRspDTO;
        }
        Map<Long, CompanyUserKeyFieldRspDTO> userMap = companyUserService
                .listByIds(invoiceCompanys.stream().map(
                        e -> e.getUserId()).collect(Collectors.toList()));

        Map<Long, CompanyKeyFieldDTO> companyRspDTOMap = companyService.listByIds(
                invoiceCompanys.stream().map(e -> e.getId()).collect(Collectors.toList()));
        List<SubstituteInvoiceCompanyQueryRspDTO> companyQueryRspDTOS = invoiceCompanys
                .stream().map(invoiceCompany -> {
                    if (userMap.containsKey(invoiceCompany.getUserId())) {
                        invoiceCompany.setUserName(userMap
                                .get(invoiceCompany.getUserId()).getName());
                        invoiceCompany.setUserEmail(userMap
                                .get(invoiceCompany.getUserId()).getEmail());
                        invoiceCompany.setUserMobile(userMap
                                .get(invoiceCompany.getUserId()).getMobile());
                        invoiceCompany.setUserQq(userMap
                                .get(invoiceCompany.getUserId()).getQq());
                    }
                    if (companyRspDTOMap.containsKey(invoiceCompany.getId())) {
                        invoiceCompany.setCompanyName(companyRspDTOMap
                                .get(invoiceCompany.getId()).getName());
                        invoiceCompany.setSupplierSettlementMethod(companyRspDTOMap
                                .get(invoiceCompany.getId()).getSupplierSettlementMethod());
                    }
                    return invoiceCompany;
                }).collect(Collectors.toList());
        pageRspDTO.setRecords(companyQueryRspDTOS);
        return pageRspDTO;
    }

    @Override
    public Long create(SubstituteInvoiceCompanyAddReqDTO reqDTO) {
        return substituteInvoiceCompanyService.create(reqDTO);
    }

    @Override
    public void update(SubstituteInvoiceCompanyUpdateReqDTO reqDTO) {
        substituteInvoiceCompanyService.update(reqDTO);
    }

    @Override
    public void enable(Long id, Long employeeId) {
        substituteInvoiceCompanyService.enable(id, employeeId);
    }
}
